# coding=UTF-8
import cgi
import datetime
import wsgiref.handlers

from google.appengine.ext import db
from google.appengine.api import users
from google.appengine.ext import webapp

class Directory(db.Model):
  content = db.StringProperty(multiline=True)
  uploadDate = db.DateTimeProperty(auto_now_add=True)
  createDate = db.DateTimeProperty(auto_now_add=True)

class TextFile(db.Model):
  content = db.StringProperty(multiline=True)
  uploadDate = db.DateTimeProperty(auto_now_add=True)
  createDate = db.DateTimeProperty(auto_now_add=True)

class ImageFile(db.Model):
  url = db.StringProperty(multiline=True)
  uploadDate = db.DateTimeProperty(auto_now_add=True)
  createDate = db.DateTimeProperty()

#===========================================================

class Public(webapp.RequestHandler):
  def get(self):  
    mainPageFile = open("index.htt", "r", 8192)
    self.response.out.write(mainPageFile.read())

class CMS(webapp.RequestHandler):
  def get(self):  
    

    mainPageFile = open("upload.htt", "r", 8192)
    self.response.out.write(mainPageFile.read())

    for textFile in db.Model.get("SELECT * FROM TextFile"):
      self.response.out.write(textFile.uploadDate)

  def post(self):
    if self.request.get('content1'):
      textFile1 = TextFile()
      textFile1.content = self.request.get('content1')
      textFile1.put()

    if self.request.get('content2'):
      textFile2 = TextFile()
      textFile2.content = self.request.get('content2')
      textFile2.put()

    if self.request.get('content3'):
      textFile3 = TextFile()
      textFile3.content = self.request.get('content3')
      textFile3.put()

    if self.request.get('content4'):
      textFile4 = TextFile()
      textFile4.content = self.request.get('content4')
      textFile4.put()

    if self.request.get('content5'):
      textFile5 = TextFile()
      textFile5.content = self.request.get('content5')
      textFile5.put()

    self.redirect('/cms')


application = webapp.WSGIApplication([
  ('/cms', CMS),
  ('.*', Public)
], debug=True)


def main():
  wsgiref.handlers.CGIHandler().run(application)


if __name__ == '__main__':
  main()
